package cn.taobaoke.restful.service;

import cn.taobaoke.restful.domain.DocumentEntity;
import cn.taobaoke.restful.domain.DocumentRepository;
import cn.taobaoke.restful.exception.TBKException;
import cn.taobaoke.restful.util.DateUtil;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author yangzuo
 * @since 2017-04-18 22:08
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DocumentService {

    public static final String DELIMITER = ";";

    public DocumentEntity saveDocument(final DocumentEntity entity, final Long domainId) {
        if (entity.getImages() != null) {
            entity.setImage(String.join(DELIMITER, entity.getImages()));
        }
        String currentDt = DateUtil.currentDateTimeString();
        entity.setPublishTime(currentDt);
        entity.setCreateTime(currentDt);
        entity.setDomainId(domainId);
        return documentRepository.save(entity);
    }

    public DocumentEntity getDocumentById(final Long id, final Long domainId) {
        val entityOpt = documentRepository.findById(id, domainId);
        if (! entityOpt.isPresent()) {
            throw new TBKException("不存在的文案ID ["+ id +"]");
        }
        val entity = entityOpt.get();
        makeImageList(entity);
        return entity;
    }

    public void deleteDocumentById(final Long id, final Long domainId) {
        val entityOpt = documentRepository.findById(id, domainId);
        if (! entityOpt.isPresent()) {
            throw new TBKException("不存在的文案ID ["+ id +"]");
        }
        val entity = entityOpt.get();
        entity.setIsDelete(true);
        documentRepository.save(entity);
    }

    public DocumentEntity updateDocument(final Long id, final DocumentEntity entity, final Long domainId) {
        val existEntityOpt = documentRepository.findById(id, domainId);
        if (! existEntityOpt.isPresent()) {
            throw new TBKException("不存在的文案ID ["+ id +"]");
        }

        val existEntity = existEntityOpt.get();
        if (entity.getImages() != null) {
            existEntity.setImage(String.join(DELIMITER, entity.getImages()));
        }
        if (entity.getDescription() != null) {
            existEntity.setDescription(entity.getDescription());
        }
        if (entity.getLinkAddress() != null) {
            existEntity.setLinkAddress(entity.getLinkAddress());
        }
        if (entity.getTemplate() != null) {
            existEntity.setTemplate(entity.getTemplate());
        }
        existEntity.setPublishTime(DateUtil.currentDateTimeString());

        return documentRepository.save(existEntity);
    }

    public Page<DocumentEntity> pagingQueryDocuments(final Pageable pageable, final Long domainId) {
        Page<DocumentEntity> pages = documentRepository.findDocumentsByPage(domainId, pageable);
        for (DocumentEntity entity : pages) {
            makeImageList(entity);
        }
        return pages;
    }

    private void makeImageList(final DocumentEntity entity) {
        if (entity.getImage() != null) {
            entity.setImages(Arrays.asList(entity.getImage().split(DELIMITER)));
            entity.setImage(null);
        }
    }

    private final @NonNull DocumentRepository documentRepository;

    private static Logger logger = LogManager.getLogger(DocumentService.class);
}
